package jnBase.components
{
	import laya.display.Sprite;
	import laya.events.Event;
	import laya.events.Keyboard;
	import laya.maths.Point;
	import laya.resource.HTMLCanvas;
	import laya.resource.Texture;
	import laya.utils.Pool;
	
	public class BaseBrush extends Sprite
	{
		private var isDown:Boolean=false;
		private var lastPoint:Point;
		private var baseBrush:Sprite;
		/*线颜色*/
		public  var lineColor:String="#ff0000";
		/*线宽*/
		private  var _lineWidth:Number=3;
		/**
		 *橡皮擦 
		 */		
		public var isEraser:Boolean=false;
		private var _isRect:Boolean=false;
		/**
		 *铅笔模式 
		 */		
		public var isAnyDraw:Boolean=true;
		private var _isLine:Boolean=false;
		private var _isCircle:Boolean=false;
		private var gap:Number=3;
		private var oldScale:Number=1;
		private var sizes:Number=10;
		private var per:Number;
		public function BaseBrush($wid:Number,$heg:Number)
		{
			super();
			this.cacheAs="bitmap";
			size($wid,$heg);
		}

		public function get lineWidth():Number
		{
			return _lineWidth;
		}

		public function set lineWidth(value:Number):void
		{
			_lineWidth = value;
			sizes=value*2;
		}

		/**
		 *圆 
		 */
		public function get isCircle():Boolean
		{
			return _isCircle;
		}

		/**
		 * @private
		 */
		public function set isCircle(value:Boolean):void
		{
			_isCircle = value;
			if(_isCircle){
				isRect=false;
				isLine=false;
				isAnyDraw=false;
				isEraser=false;
			}
		}

		/**
		 *直线 
		 */
		public function get isLine():Boolean
		{
			return _isLine;
		}

		/**
		 * @private
		 */
		public function set isLine(value:Boolean):void
		{
			_isLine = value;
			if(_isLine){
				isCircle=false;
				isRect=false;
				isAnyDraw=false;
				isEraser=false;
			}
		}

		/**
		 *矩形 
		 */
		public function get isRect():Boolean
		{
			return _isRect;
		}

		/**
		 * @private
		 */
		public function set isRect(value:Boolean):void
		{
			_isRect = value;
			if(_isRect){
				isCircle=false;
				isLine=false;
				isAnyDraw=false;
				isEraser=false;
			}
		}
		
		public function clear():void{
			this.graphics.clear();
			while(this.numChildren>0){
				var child:*=this.removeChildAt(0);
				child.destroy();
			}
		}
		
		override public function onEnable():void
		{
			// TODO Auto Generated method stub
			super.onEnable();
			lastPoint=new Point();
			this.on(Event.MOUSE_DOWN,this,mouseDown);
			this.on(Event.MOUSE_OUT,this,mouseUp);
			this.on(Event.MOUSE_UP,this,mouseUp);
			this.once(Event.REMOVED,this,removed);
			Laya.timer.frameLoop(1,this,moveing);
			Laya.stage.on(Event.KEY_UP,this,mouseHandler);
		}
		
		private function mouseHandler(e:Event):void
		{
			// TODO Auto Generated method stub
			trace(e.keyCode);
			switch(e.keyCode)
			{
				case Keyboard.B:
				{
					isEraser=false;
					break;
				}
				case Keyboard.E:
				{
					isEraser=true;
					break;
				}
				case Keyboard.UP:
				{
					lineWidth+=2;
					break;
				}
				case Keyboard.DOWN:
				{
					lineWidth-=2;
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function removed():void
		{
			// TODO Auto Generated method stub
			this.off(Event.MOUSE_DOWN,this,mouseDown);
			this.off(Event.MOUSE_OUT,this,mouseUp);
			this.off(Event.MOUSE_UP,this,mouseUp);
//			this.off(Event.MOUSE_MOVE,this,mouseMove);
			Laya.timer.clear(this,moveing);
		}
		
		private function mouseDown(e:Event):void
		{
			// TODO Auto Generated method stub
			isDown=true;
			lastPoint.setTo(mouseX,mouseY);
			baseBrush=new Sprite();
			this.addChild(baseBrush);
		}
		private function moveing():void
		{
			// TODO Auto Generated method stub
			if(!isDown){
				return ;
			}
			if(isAnyDraw || isEraser){
				anyDraw();
				lastPoint.setTo(mouseX,mouseY);
			}
			else if(isRect) drawRect(mouseX,mouseY);
			else if(isCircle) drawCircle(mouseX,mouseY);
			else if(isLine) drawLine(mouseX,mouseY);
		}
		private function drawLine($mousex:Number, $mousey:Number):void
		{
			// TODO Auto Generated method stub
			baseBrush.graphics.clear();
			baseBrush.graphics.drawLine(lastPoint.x,lastPoint.y,$mousex,$mousey,lineColor,lineWidth);
		}
		private function drawCircle($mousex:Number, $mousey:Number):void
		{
			// TODO Auto Generated method stub
			baseBrush.graphics.clear();
			var r:Number=Math.sqrt(Math.pow($mousex-lastPoint.x,2)+Math.pow($mousey-lastPoint.y,2))/2;
			baseBrush.graphics.drawCircle(lastPoint.x+($mousex-lastPoint.x)/2,lastPoint.y+($mousey-lastPoint.y)/2,r,null,lineColor,lineWidth);
		}
		private function drawRect($mousex:Number,$mousey:Number):void{
			baseBrush.graphics.clear();
			baseBrush.graphics.drawRect(lastPoint.x,lastPoint.y,$mousex-lastPoint.x,$mousey-lastPoint.y,null,lineColor,lineWidth);
		}
		/**
		 *铅笔模式 
		 * 
		 */		
		private function anyDraw():void
		{
			// TODO Auto Generated method stub
			if(isEraser){
				baseBrush.blendMode = "destination-out";
				baseBrush.graphics.drawCircle(mouseX,mouseY,lineWidth/2,lineColor);
				baseBrush.graphics.drawPath(0,0,[["moveTo",lastPoint.x,lastPoint.y],["lineTo",mouseX,mouseY],],null,{strokeStyle:lineColor,lineWidth:lineWidth*10});
				lastPoint.setTo(mouseX,mouseY);
			}
			else godDraw();
		}
		private function godDraw():void{
			
			//更新起始点，使当前坐标成为下一个起始点
			var dx:Number = mouseX - lastPoint.x;
			var dy:Number = mouseY - lastPoint.y;
			var dd:Number =Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
			per=1.5-dd*0.01;
			if(per<0.08){
				per=0.08;
			}
			
			per=(oldScale+per)/2;
			
			//trace("per:"+per);
			var wid:Number=lineWidth*per;
			//trace("width:"+wid);
			baseBrush.graphics.drawCircle(mouseX,mouseY,wid/2,lineColor);
			baseBrush.graphics.drawPath(0,0,[["moveTo",lastPoint.x,lastPoint.y],["lineTo",mouseX,mouseY],],null,{strokeStyle:lineColor,lineWidth:wid});
			lastPoint.setTo(mouseX,mouseY);
			oldScale=per;
		}
		/**
		 *毛笔笔触算法 
		 * 
		 */		
		private function drawMaobiBrush():void
		{
			// TODO Auto Generated method stub
			var dx:Number = mouseX - lastPoint.x;
			var dy:Number = mouseY - lastPoint.y;
			var dd:Number =Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
			var n:int = Math.ceil(dd / gap);
			
			/**2014-12-20修改**/
			var scale:Number = 0.5 - dd * 0.02;
			if (scale > 1.2)
			{
				scale = 1.2;
			}
			else if (scale < 0.08)
			{
				scale = 0.08;
			}
			scale = (oldScale + scale) / 2;
			var scaleBili:Number = (oldScale - scale) / n;
			
			/**2014-12-20修改**/
			
			for (var i:int = 0; i < n; i++) {
				var d:Sprite = Pool.getItemByClass("brush",Sprite);
				d.graphics.drawCircle(0,0,lineWidth*2,"#ff0000");
				var sc:Number=oldScale-i*scaleBili;
				//setmcsize(d,sc);
				d.x = dx / n * (i+1)+ lastPoint.x;
				d.y = dy / n * (i+1) + lastPoint.y;
				baseBrush.addChild(d);
				//				d.filters = new Array(bf);
				d.alpha = 0.8;
				d.scaleX = d.scaleY = sc+lineWidth*2*0.02;
				d.x = (dx / n) * (i + 1) + lastPoint.x;
				d.y = (dy / n) * (i + 1) + lastPoint.y;
			}
			//更新起始点，使当前坐标成为下一个起始点
			oldScale = scale;
			lastPoint.setTo(mouseX,mouseY);
		}
		private function tobitmap():void{
			var htmlCanvas:HTMLCanvas=this.drawToCanvas(this.width,this.height,0,0);
			this.graphics.drawTexture(new Texture(htmlCanvas));
			while(this.numChildren>0){
				var child:Sprite=this.getChildAt(0) as Sprite;
				child.destroy();
			}
		}
		private function mouseUp(e:Event):void
		{
			// TODO Auto Generated method stub
			if(!isDown){
				return ;
			}
			isDown=false;
			if(isEraser){
				return ;
			}
//			baseBrush.cacheAs='bitmap';
		}
	}
}